package com.rongke.web.interceptor;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.model.Admin;
import com.rongke.redis.CacheUtil;
import com.rongke.service.AdminService;
import com.rongke.web.ajaxUtil.AjaxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by chengwenwen on 2016/10/19.
 * URL拦截
 */
public class URLInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private AdminService adminService;
    @Autowired
    private CacheUtil cacheUtil;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //LoginCheck Annotation，实现登陆验证
        Cookie cookie[]=request.getCookies();
        String sessionId=null;
        if(cookie!=null){
            for (int i = 0; i < cookie.length; i++)
               {
                   Cookie coo = cookie[i];
                  if ("sid".equals(coo.getName()))
                      {
                          sessionId=coo.getValue();
                          break;
                      }
           }
        }else {
          return true;
        }
        String referer="";
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            if(key.equals("referer")){
                referer=value;
                break;
            }
        }
        if(sessionId==null){
            Map<String,String> resultMap = new HashMap<String, String>();
            resultMap.put("login_status", "300");
            resultMap.put("msg", "登录过时或未登录，请重新登录");//当前用户没有登录！
            AjaxUtil.out(response, resultMap);
            return false;
        }
        EntityWrapper<Admin> ew=new EntityWrapper<>();
        ew.eq("token",sessionId);
        System.out.print("token.............."+sessionId);
        Admin admin=adminService.selectOne(ew);
        System.out.print("admin.............."+admin);
        if(admin==null){
            Map<String,String> resultMap = new HashMap<String, String>();
            resultMap.put("login_status", "300");
            resultMap.put("msg", "您的账号已在另一地方登录，您已被迫下线");//当前用户没有登录！
            AjaxUtil.out(response, resultMap);
            return false;
        }
        Set<String> menu=cacheUtil.getSet("menu"+admin.getLoginName());
        String arr[]=referer.split("/");
        if(!menu.contains(arr[arr.length-1])){
            Map<String,String> resultMap = new HashMap<String, String>();
            resultMap.put("login_status", "300");
            resultMap.put("msg", "对不起，您没有使用权限");//当前用户没有登录！
            AjaxUtil.out(response, resultMap);
            return false;
        }
        return true;
    }
public static void main(String str[]){
    String str1="https://app.miduu.com/admin/index.html";
    String arr[]=str1.split("/");
    System.out.print(arr[arr.length-1]);
}

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        Cookie cookie[]=request.getCookies();
        System.out.print(cookie);

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

}
